/**
 * Created by Administrator on 2017/9/12.
 */
var React = require('react');
var Bheader = require('../widget/Bheader');
var ajax = require('../tools/ajax');
var Icon = require('../components/Icon');
var MsgBox = require('../components/MsgBox');


module.exports = React.createClass({
    getInitialState: function () {
        return {
            newProInfo:[],
            is_newperson:0
        }

    },
    componentDidMount: function () {
        this.getNewProInfo();
        this.getUserInfoByUid();
        setTimeout(this.iscroll,500);
    },

    /**iScroll插件*/
    iscroll(){
        var p =this;
        var options = {
            // 默认iscroll会拦截元素的默认事件处理函数，我们需要响应onClick，因此要配置
            preventDefault: false,
            // 禁止缩放
            zoom: false,
            // 支持鼠标事件，因为我开发是PC鼠标模拟的
            mouseWheel: true,
            // 滚动事件的探测灵敏度，1-3，越高越灵敏，兼容性越好，性能越差
            probeType: 3,
            // 拖拽超过上下界后出现弹射动画效果，用于实现下拉/上拉刷新
            bounce: true,
            // 展示滚动条
            scrollbars: false
        };
        p.myScroll = new IScroll("#ScrollContainer",options);
    },
    /**获取用户信息*/
    getUserInfoByUid(){
        var p =this;
        var userId = localStorage.getItem("userId");
        ajax("POST","getUserInfoByUid",{userId:userId},function(dt){
            if(dt&&dt.success){
                var obj = dt.data[0];
                p.setState({is_newperson:obj.is_newperson});
            }
        })
    },
    /**获取新人专享商品信息*/
    getNewProInfo(){
        var p =this;
        ajax("POST","getNewProInfo",{},function(dt){
            if(dt&&dt.success){
                var objs = dt.data;
                p.setState({newProInfo:objs});
                console.log(objs);
            }
        })
    },
    /**跳转页面*/
    action: function (e){
        var action = e.currentTarget.getAttribute("action");
        var goods_id = e.currentTarget.getAttribute("data");
        if(action == "#newpeopleGoodDetail"){
            if(this.state.is_newperson != 0){
                this.refs.MsgBox.setState({
                    type: "no",
                    timeout:1500,
                    show:true,
                    word:"您已在平台购买过商品,是我们的老会员!"
                })
            }else{
                localStorage.setItem("goods_id",goods_id);
                location.href = action;
            }
        }else{
            location.href = action;
        }
    },
    render: function () {
        var p = this;
        var newProInfo = p.state.newProInfo;
        var newProInfoList = newProInfo.map(function(obj,index){
            var goods_img = obj.goods_img.split(",");
            return(
                <li key={index} className="newExclusive">
                    <div><img src={goods_img[0]}/></div>
                    <p className="name">{obj.goods_name}</p>
                    <p>
                        <span className="c-h">新人专享价</span>
                        <span className="c-r">￥{obj.newperson_price}</span>
                        <span className="del-price">￥{obj.shop_price}</span>
                    </p>
                    <center><button onClick={p.action} action="#newpeopleGoodDetail" data={obj.id} data-point={obj.point}>马上抢</button></center>
                </li>
            )
        });
        return (
            <div className="main"  id="main">
                <Bheader theme={p.state.theme} title="新人专享" headr="false" ></Bheader>
                <div id="ScrollContainer">
                    <div className="shopProWrap" style={{minHeight:"31.18rem"}}>
                        <div id="fenClassPro">
                            <div id="fenClassProImg"><img src="http://115.29.198.39:8080/upload/xinpin/18078352232392983.jpg" /></div>
                        </div>
                        <p className="blankSet"></p>
                        <div id="jifenExchange">
                            <div className="jifenExchangeText"><span>新人专享区</span></div>
                            <ul className="jifenExchangePage">
                                {newProInfoList}
                            </ul>
                        </div>
                    </div>
                </div>
                <MsgBox ref="MsgBox"></MsgBox>
            </div>
        )
    }
});

